package com.zjdiepu.www.manage.api.yw;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.AddedStatus1;
import com.zjdiepu.www.base.enums.status.BusiStatus;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.ibiz.yw.IYwFundApplyService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwFundApply;
import com.zjdiepu.www.model.yw.ext.YwFundApplyExt;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;

/**
 * 申请垫资Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月09日 09时29分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "YwFundApplyController", produces="application/json;charset=UTF-8")
@Api(tags = {"申请垫资"}, produces="application/json;charset=UTF-8")
public class YwFundApplyController extends BaseController {
    @Resource
    private IYwFundApplyService ywFundApplyService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private YwBusinessVoMapper ywBusinessVoMapper;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: signContractList <br/>
     * @Description: 精确定位客户申请垫资 <br/>
     * @param ywBusinessExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "精确定位客户申请垫资",response = YwBusiness.class, httpMethod="POST", tags={"申请垫资"})
    @RequestMapping(value = "applyLoanList", method = RequestMethod.POST)
    public ApiResponse applyLoanList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser sysUser = (SysUser)getCurrentUser();
        PageVo page = getPage(ywBusinessQueryRequest);
        ywBusinessQueryRequest.setBaseWhere();
        ywBusinessQueryRequest.setBankLoan(sysUser.getLoanBank());
        ywBusinessQueryRequest.setCodeUnits(sysUser.getCodeUnits());
        ywBusinessQueryRequest.setNeedLimit(true);
    	if(StringUtils.isBlank(ywBusinessQueryRequest.getSidx())) {
    		ywBusinessQueryRequest.setOrderSql("B.`SUBMIT_TIME` DESC");
    	}
    	Boolean applyStatus = ywBusinessQueryRequest.getApplyStatus();
    	if(null == applyStatus) {// 1.按揭日报表已登记  2.财务一审核中 3.已退款
    		ywBusinessQueryRequest.addAddedStatus1(AddedStatus1.AN_JIE_RI_BAO_BIAO_YI_DENG_JI.getCode());
    		ywBusinessQueryRequest.addAddedStatus1(AddedStatus1.CAI_WU_YI_SHEN_HE_ZHONG.getCode());
    		ywBusinessQueryRequest.addAddedStatus1(AddedStatus1.YI_TUI_KUAN.getCode());
    	} else if(applyStatus){
    		ywBusinessQueryRequest.addAddedStatus1(AddedStatus1.CAI_WU_YI_SHEN_HE_ZHONG.getCode());
    	} else {
    		ywBusinessQueryRequest.addAddedStatus1(AddedStatus1.AN_JIE_RI_BAO_BIAO_YI_DENG_JI.getCode());
    		ywBusinessQueryRequest.addAddedStatus1(AddedStatus1.YI_TUI_KUAN.getCode());
    	}
    	String whereSql = "AND ((B.`BUSI_STATUS` >= " + BusiStatus.SHEN_PI_WAN_CHENG.getCode() 
				+ " AND B.`CAR_TYPE` = '01') OR (B.`BUSI_STATUS` >= " + BusiStatus.YI_GUO_HU_SHEN_PI.getCode() 
				+ " AND B.`CAR_TYPE` = '02')) ";
    	ywBusinessQueryRequest.setWhereSql(whereSql);
    	
    	int records = ywBusinessService.countLessOptionsByExt(ywBusinessQueryRequest);
    	page.setRecords(records);
    	if (records > 0) {
    		List<YwBusiness> ywBusinesss = ywBusinessService.selectLessOptionsByExt(ywBusinessQueryRequest);
    		ywBusinessVoMapper.mapper(ywBusinesss);
    		page.setRows(ywBusinesss);
    	} else {
    		page.setRows(CollectionInitUtil.emptyList());
    	}
    	return ApiResponse.success(page);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 申请垫资 <br/>
     * @param applyFund
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "申请垫资", response = Boolean.class, httpMethod="POST", tags={"申请垫资"})
    @RequestMapping(value = "applyFund", method = RequestMethod.POST)
    public ApiResponse applyFund(@Validated(Add.class) @ModelAttribute YwFundApply ywFundApply,HttpServletRequest request) {
    	SysUser currentUser =(SysUser)getCurrentUser();
    	HttpSession session = request.getSession();      
		ServletContext  application  = session.getServletContext();  
		String serverRealPath = application.getRealPath("/WEB-INF/configure/fkshConfV001.json");
        return ywFundApplyService.saveApplyFund(ywFundApply, currentUser,serverRealPath);
    }
    
    

    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 新增申请垫资 <br/>
     * @param ywFundApply
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增申请垫资", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute YwFundApply ywFundApply) {
    	AbstractSysUser currentUser = getCurrentUser();
    	ywFundApply.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	ywFundApply.setModifierName(currentUser.getName());
        ywFundApplyService.saveUpdateAndDelete(ywFundApply, OperateType.add);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: remove  <br/>
     * @Description: 删除申请垫资<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除申请垫资", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	ywFundApplyService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: disable  <br/>
     * @Description: 禁用申请垫资<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用申请垫资", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	ywFundApplyService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: enable <br/>
     * @Description: 启用申请垫资<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用申请垫资", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	ywFundApplyService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: modify  <br/>
     * @Description: 修改申请垫资 <br/>
     * @param ywFundApply
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改申请垫资", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute YwFundApply ywFundApply) {
    	AbstractSysUser currentUser = getCurrentUser();
    	ywFundApply.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	ywFundApply.setModifierName(currentUser.getName());
    	ywFundApplyService.saveUpdateAndDelete(ywFundApply, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询申请垫资<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询申请垫资", response = YwFundApply.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       YwFundApply ywFundApply = ywFundApplyService.selectById(id);
       if(null == ywFundApply) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(ywFundApply);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 条件查询申请垫资 <br/>
     * @param ywFundApplyExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询申请垫资",response = YwFundApply.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute YwFundApplyExt ywFundApplyExt){
        ywFundApplyExt.notDeleted();
        PageVo page = getPage(ywFundApplyExt);
        int records = ywFundApplyService.countByExt(ywFundApplyExt);
        page.setRecords(records);
        if (records > 0) {
            List<YwFundApply> ywFundApplys = ywFundApplyService.selectByExt(ywFundApplyExt);
            page.setRows(ywFundApplys);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

}
